﻿
import YouHP.Graphics.*;

class PieStyle extends ChartBase
{
    //图例数据
	var mLegendLeft : Number = 5;				//图例的左边界	
	var mLegendTop : Number = 15;				//图例数据顶部偏移
	var mLegendWidth : Number = 150;			//图例宽度	 
	var mLegendYGap : Number = 18;				//图例间Y轴上的间距
	var mLegendXGap : Number = 5;				//图例内容相对于图例边界的偏移	
	
	//图表
	var mChartLeft : Number = 60 ;
	var mChartWidth : Number = 0;
	var mChartTop : Number = 10;
	var mChartHeight : Number = 0;
	var mChartRight : Number = 0;	
	var mCenterPointX : Number = 0;
	var mCenterPointY : Number = 0;
	var mRadius : Number = 70;
	
	//图表数据
	var lables:Array = new Array();
	var links:Array = new Array();
	var colours:Array = new Array();	
	var values:Array = new Array();
	
	var mTotalVal:Number = 0;
	
	 //每个显示点对应的MovieClip，以及相应的ToolTip
	 var mChildmcArr : Array;			
	
	function PieStyle(lv : Object)
    {
		mClip = _root.createEmptyMovieClip("PieStyle", _root.getNextHighestDepth());
        mData = lv;		
        
		//提取图表数据
        ParseData();
		
		//设置图例
		DrawLegend();
		
		//画图
        DrawChart();
	}
	
	private function ParseData()
	{
		var varArr = mData["Var"].split(";");
		var dataCnt : Number = varArr[1];
		var dataArr : Array ;
		
		for(var i=0; i<dataCnt; i++)
		{
			dataArr = mData["Data_" + i].split(";");
						
			lables.push([dataArr[0]]);
			colours.push([dataArr[1]]);
			values.push([dataArr[2]]);			
			
			mTotalVal += Number(dataArr[2]);
		}
		
		mCenterPointX = mLegendLeft + mLegendWidth + mChartLeft + mRadius ;
		mCenterPointY = mChartTop + mRadius;
	}
	      
    public function DrawLegend()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);

        var pen = new Pen(1.0,0x000000,100);
		var titlePen = new Pen(1.0,0xcccccc,100);
		var brush = new SolidBrush(0x000000, 100);
        var startx = mLegendLeft + mLegendXGap;
        var starty = mLegendTop;
        var y = 0;
		
		starty = 1.5*mLegendYGap;						
        for(var i=0; i < lables.length; i++)
        {
            y = starty + i * mLegendYGap;
            
            brush.setColor(colours[i][0]);
            gs.FillRect(brush, startx + 10, y, 12, 12);
            gs.DrawRect(pen, startx + 10, y, 12, 12);
            
           	var label:TextField = mc.createTextField("option_" + i ,mc.getNextHighestDepth(),
              	startx + 25, y-3, 0, 0);
            var myformat = new TextFormat();
        	myformat.color = 0xff0000;
		    myformat.align = "left";
		    label.setTextFormat(myformat);
		            
        	label.html = true;
		    label.autoSize = "left";
		    label.htmlText = lables[i][0];
        }
						
		gs.DrawRect(pen, startx, starty - mLegendYGap/2.0, mLegendWidth - 4*mLegendXGap, 1.5*mRadius);      
    }
	
	private function DrawSector(pen:Pen, brush:SolidBrush, startDegree:Number, endDegree:Number, value:String, pointCnt:Number)
	{
		var mc:MovieClip = _root.createEmptyMovieClip( "Tip_" + pointCnt, _root.getNextHighestDepth() );
		var gs:Graphics = new Graphics(mc);		
		var link:String = "" ;	//对应的超链接
		
		mc.ToolTip(value, link);
	
		gs.DrawSector(pen, mCenterPointX, mCenterPointY, mRadius, mRadius, startDegree, endDegree);
		gs.FillSector(brush, mCenterPointX, mCenterPointY, mRadius, mRadius, startDegree, endDegree);
		
		this.mChildmcArr.push(mc);				
	}

    public function DrawChart()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);

        var pen = new Pen(1.0,0xffffff,100);
		var titlePen = new Pen(1.0,0xcccccc,100);
		var brush = new SolidBrush(0x000000, 100);
		
		var percent : Number = 0.0;
		var startDegree:Number = -45;
		var endDegree:Number = 0;
		
		var cnt:Number = 0;
		var txtTip:String ;
		var num:Number = 0;
		
		gs.DrawCircle(pen, mCenterPointX, mCenterPointY, mRadius);
		for(var i=0; i<values.length; i++)
		{
			percent = values[i][0]*1.0/mTotalVal ;
			endDegree = startDegree + 360 * percent;
			brush.setColor(colours[i][0]);
			
			pen.setThickness(2);
			num = values[i][0]*100/mTotalVal;
			txtTip = "" + num + "%" ;
			DrawSector(pen, brush, startDegree, endDegree, txtTip, cnt++);
			
			startDegree = endDegree;
		}
    }
}